package com.ruoyi.bysj.controller;

import java.util.List;

import com.ruoyi.bysj.domain.SysGoods;
import com.ruoyi.bysj.service.ISysGoodsService;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bysj.domain.SysDelivery;
import com.ruoyi.bysj.service.ISysDeliveryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货物配送Controller
 * 
 * @author laow
 * @date 2021-10-19
 */
@RestController
@RequestMapping("/bysj/delivery")
public class SysDeliveryController extends BaseController
{
    @Autowired
    private ISysDeliveryService sysDeliveryService;

    @Autowired
    private ISysGoodsService sysGoodsService;

    /**
     * 查询货物配送列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDelivery sysDelivery)
    {
        startPage();
        sysDelivery.setCreateName(getUsername());
        List<SysDelivery> list = sysDeliveryService.selectSysDeliveryList(sysDelivery);
        return getDataTable(list);
    }

    /**
     * 导出货物配送列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:export')")
    @Log(title = "货物配送", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDelivery sysDelivery)
    {
        List<SysDelivery> list = sysDeliveryService.selectSysDeliveryList(sysDelivery);
        ExcelUtil<SysDelivery> util = new ExcelUtil<SysDelivery>(SysDelivery.class);
        return util.exportExcel(list, "货物配送数据");
    }

    /**
     * 获取货物配送详细信息
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:query')")
    @GetMapping(value =   { "/", "/{deliveryId}" } )
    public AjaxResult getInfo(@PathVariable("deliveryId") Long deliveryId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("goods", sysGoodsService.selectGoodsAll());
        if (StringUtils.isNotNull(deliveryId))
        {
            ajax.put(AjaxResult.DATA_TAG, sysDeliveryService.selectSysDeliveryByDeliveryId(deliveryId));
            ajax.put("goodsIds", sysDeliveryService.selectGoodsListBydeliveryId(deliveryId));
        }
        return ajax;
        /*return AjaxResult.success(sysDeliveryService.selectSysDeliveryByDeliveryId(deliveryId));*/
    }

    /**
     * 新增货物配送
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:add')")
    @Log(title = "货物配送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDelivery sysDelivery)
    {
        sysDelivery.setCreateName(getUsername());
        return toAjax(sysDeliveryService.insertSysDelivery(sysDelivery));
    }

    /**
     * 修改货物配送
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:edit')")
    @Log(title = "货物配送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDelivery sysDelivery)
    {
        sysDelivery.setDeliveryName(getUsername());
        return toAjax(sysDeliveryService.updateSysDelivery(sysDelivery));
    }

    /**
     * 删除货物配送
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:remove')")
    @Log(title = "货物配送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deliveryIds}")
    public AjaxResult remove(@PathVariable Long[] deliveryIds)
    {
        return toAjax(sysDeliveryService.deleteSysDeliveryByDeliveryIds(deliveryIds));
    }

    /**
     * 查询仓库存储列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:delivery:goodsList')")
    @GetMapping("/goodsList")
    public TableDataInfo goodsList(SysGoods sysGoods)
    {
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        return getDataTable(list);
    }
}
